package com.newtype.meeting.client.widget.frontend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.MeetingStart;

public class UnfinishedMeetingTabPanel extends ContentPanel {
	private ColumnModel cm;

	public UnfinishedMeetingTabPanel(ListStore<MeetingStart> store) {
		setFrame(true);
		setLayout(new FitLayout());
		setHeaderVisible(false);

		final DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");

		GridCellRenderer<MeetingStart> startDate = new GridCellRenderer<MeetingStart>() {
			@Override
			public Object render(MeetingStart model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<MeetingStart> store, Grid<MeetingStart> grid) {
				Date val = (Date) model.get(property);
				Date today = new Date();

				String style = val.before(today) ? "red"
						: GXT.isHighContrastMode ? "#00ff5a" : "green";
				String v = dtf.format(val);

				return "<span qtitle='"
						+ cm.getColumnById(property).getHeader() + "' qtip='"
						+ v + "' style='font-weight: bold;color:" + style
						+ "'>" + v + "</span>";
			}

		};

		RowNumberer r = new RowNumberer();

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		configs.add(r);

		ColumnConfig column = new ColumnConfig();
		column.setId("subject");
		column.setHeader("主题");
		column.setWidth(400);
		column.setRowHeader(true);
		configs.add(column);

		column = new ColumnConfig("startDate", "会议时间", 175);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setRenderer(startDate);
		configs.add(column);

		column = new ColumnConfig("submitDate", "存档时间", 200);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setDateTimeFormat(DateTimeFormat
				.getFormat("yyyy-MM-dd HH:mm:ss"));
		configs.add(column);

		cm = new ColumnModel(configs);

		final Grid<MeetingStart> grid = new Grid<MeetingStart>(store, cm);
		grid.addPlugin(r);
		grid.setAutoExpandColumn("subject");
		grid.setBorders(false);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		grid.setStateful(true);

		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<MeetingStart>>() {
					public void handleEvent(
							SelectionChangedEvent<MeetingStart> be) {
						if (be.getSelection().size() > 0) {
							MeetingStart start = be.getSelection().get(0);
							Dispatcher.forwardEvent(AppEvents.ToStartMeeting,
									start);
						}
					}
				});

		ContentPanel cp = new ContentPanel();
		cp.setBodyBorder(false);
		cp.setHeaderVisible(false);
		cp.setLayout(new FitLayout());
		cp.setHeight(205);
		cp.add(grid);

		VerticalPanel vp = new VerticalPanel();
		vp.setLayout(new FlowLayout(5));
		vp.setBorders(false);
		vp.setHeight(250);

		vp.add(createTitle("未完成草稿"));

		vp.add(cp);
		add(vp);
	}

	private Text createTitle(String str) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div style='line-height: 2em;'>");
		sb.append("<p style='font-size:22px;font-weight: bold;color:#0060A0;'>"
				+ str + "</p>");
		sb.append("<hr size='1'>");
		sb.append("</div>");
		Text title = new Text(sb.toString());
		return title;
	}
}
